package com.zhangxuehai.pdfviewer.ui;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Color;
import android.util.AttributeSet;
import android.util.TypedValue;
import android.view.Gravity;
import android.view.MotionEvent;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.ColorInt;
import androidx.annotation.DrawableRes;
import androidx.annotation.Nullable;
import androidx.core.widget.TextViewCompat;

import com.zhangxuehai.pdfviewer.R;

/**
 * @author 章学海
 * @date 2020/3/5
 */
@SuppressLint("AppCompatCustomView")
public class StatusView extends LinearLayout {
    boolean check;
    boolean clickChange;
    @DrawableRes
    int checkImage;
    @DrawableRes
    int uncheckImage;
    @ColorInt
    int checkTextColor;
    @ColorInt
    int uncheckTextColor;
    String checkText;
    String uncheckText;

    ImageView background;
    TextView text;

    public StatusView(Context context){
        this(context,null,0);
    }

    public StatusView(Context context, @Nullable AttributeSet attrs){
        this(context,attrs,0);
    }

    public StatusView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.StatusView, defStyleAttr, 0);
        check = a.getBoolean(R.styleable.StatusView_checked,false);
        clickChange = a.getBoolean(R.styleable.StatusView_clickChange,false);
        checkImage = a.getResourceId(R.styleable.StatusView_checkImage,0);
        uncheckImage = a.getResourceId(R.styleable.StatusView_uncheckImage,0);
        checkTextColor = a.getColor(R.styleable.StatusView_checkTextColor, Color.BLACK);
        uncheckTextColor = a.getColor(R.styleable.StatusView_uncheckTextColor, Color.BLACK);
        checkText = a.getString(R.styleable.StatusView_checkText);
        uncheckText = a.getString(R.styleable.StatusView_uncheckText);

        setOrientation(VERTICAL);
        setGravity(Gravity.CENTER);
        View v = inflate(context,R.layout.view_status,this);
        background = v.findViewById(R.id.statusViewImage);
        text = new TextView(context);
        text.setSingleLine(true);
        text.setGravity(Gravity.CENTER);
        text.setTextSize(TypedValue.COMPLEX_UNIT_SP,10);
        TextViewCompat.setAutoSizeTextTypeUniformWithConfiguration(text,6,24,1, TypedValue.COMPLEX_UNIT_SP);
        addView(text, LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);

        initUi();
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        if(clickChange){
            if(event.getAction() == MotionEvent.ACTION_UP){
                check = !check;
                initUi();
            }
            return true;
        }
        return super.onTouchEvent(event);
    }

    void initUi(){
        background.setImageResource(check?checkImage:uncheckImage);
        text.setTextColor(check?checkTextColor:uncheckTextColor);
        text.setText(check?checkText:uncheckText);
    }

    public TextView getText() {
        return text;
    }

    public boolean isCheck() {
        return check;
    }

    public void setCheck(boolean check) {
        this.check = check;
        initUi();
    }

    public boolean isClickChange() {
        return clickChange;
    }

    public void setClickChange(boolean clickChange) {
        this.clickChange = clickChange;
    }

    public int getCheckImage() {
        return checkImage;
    }

    public void setCheckImage(int checkImage) {
        this.checkImage = checkImage;
        initUi();
    }

    public int getUncheckImage() {
        return uncheckImage;
    }

    public void setUncheckImage(int uncheckImage) {
        this.uncheckImage = uncheckImage;
        initUi();
    }

    public int getCheckTextColor() {
        return checkTextColor;
    }

    public void setCheckTextColor(int checkTextColor) {
        this.checkTextColor = checkTextColor;
        initUi();
    }

    public int getUncheckTextColor() {
        return uncheckTextColor;
    }

    public void setUncheckTextColor(int uncheckTextColor) {
        this.uncheckTextColor = uncheckTextColor;
        initUi();
    }
}
